package org.test.aspect;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 代理管理器
 * Created by Hy on 2018/7/9.
 * 使用cglib提供的Enhancer#create方法创建代理对象，将intercept的参数传入ProxyChain的构造器中即可。
 */
public class ProxyManager {
public static <T> T createProxy(final Class<?> targetClass,final List<Proxy> proxyList){
   return (T) Enhancer.create(targetClass, new MethodInterceptor() {
       public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
           return new ProxyChain(targetClass,targetObject,targetMethod,methodProxy,methodParams,proxyList).doProxyChain();
       }
   });
}
}
